<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class admin_group_class extends AWS_MODEL
{
    
	/**
	 * 获得组用户权限
	 * Enter description here ...
	 * @param unknown_type $group_id
	 */
	function get_permission_by_group_id($group_id)
	{
		$group_id = intval($group_id);
		
		$group = $this->fetch_row('admin_group', 'group_id=' . $group_id );
		
		if(empty($group))
		{
			return false;
		}
		
		$data = array();
		
		foreach(explode(",", $group['permission']) as $key => $val)
		{
			$pri_arr = explode("->", $val);

			$data[$pri_arr[0]][] = $pri_arr[1];
		}
		
		return $data;
	}
	
	
	/**
	 * 获得组可见栏目
	 * Enter description here ...
	 * @param unknown_type $group_id
	 */
	function get_avail_menu_by_group_id($group_id)
	{
		$group = $this->fetch_row('admin_group', 'group_id=' . intval($group_id) );
		
		if(empty($group))
		{
			return false;
		}
		
		return $this->get_menu_by_ids($group['menu'], $group['no_menu']);
	}
	
	/**
	 * 根据栏目id集获得栏目列表
	 * Enter description here ...
	 * @param unknown_type $ids
	 */
	public function get_menu_by_ids($in_ids, $noin_ids = null)
	{
		$menu_array = (array)AWS_APP::config()->get('admin/menu');
		
		if(empty($menu_array))
		{
			return false;
		}
		
		if($in_ids != 'all')
		{
			$in_ids_arr = explode(',', $in_ids);
		}
		
		if($noin_ids)
		{
			$noin_ids_arr = explode(',', $noin_ids);
		}
		
		$menu_array = $this->menu_format($menu_array, $in_ids_arr, $noin_ids_arr, 0);
		
		return $menu_array;
	}
	
	
	function menu_format($menu_array, $in_ids, $noin_ids, $status = 0)
	{
		if(empty($menu_array))
		{
			return false;
		}
		
		foreach($menu_array as $m_id => $menu)
		{
			if($menu['status'] != $status)
			{
				unset($menu_array[$m_id]);
				continue;
			}

			if(($in_ids && (!in_array($menu['id'], $in_ids))) || ($noin_ids && (in_array($menu['id'], $noin_ids))))
			{
				unset($menu_array[$m_id]);
				continue;
			}
			
			if($menu['children'])
			{
				$menu_array[$m_id]['children'] = $this->menu_format($menu['children'], $in_ids, $noin_ids, $status);
			}
		}
		
		return $menu_array;
	}
}
